package com.yl.module.property.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yl.common.constant.Constants;
import com.yl.common.core.domain.entity.SysUser;
import com.yl.common.exception.ServiceException;
import com.yl.common.utils.NumberUtils;
import com.yl.framework.interceptor.UserContext;
import com.yl.module.file.domain.FileType;
import com.yl.module.file.domain.vo.File2VO;
import com.yl.module.file.service.FileService;
import com.yl.module.property.domain.Property;
import com.yl.module.property.domain.PropertyMaintain;
import com.yl.module.property.domain.dto.PropertyMaintainAddOrUpdDTO;
import com.yl.module.property.domain.dto.PropertyUpkeepAddOrUpdDTO;
import com.yl.module.property.domain.vo.PropertyMaintainVO;
import com.yl.module.property.mapper.PropertyUpkeepMapper;
import com.yl.module.property.service.PropertyService;
import com.yl.module.property.service.PropertyUpkeepService;
import com.yl.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.yl.module.property.domain.vo.PropertyUpkeepVO;
import com.yl.module.property.domain.dto.PropertyUpkeepDTO;
import com.yl.module.property.domain.PropertyUpkeep;
import cn.hutool.core.bean.BeanUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 资产维修记录表 Service实现类
 * @author makejava
 * @date 2024-10-23 15:54:16
 */
@Service
@Slf4j
public class PropertyUpkeepServiceImpl extends ServiceImpl<PropertyUpkeepMapper, PropertyUpkeep> implements PropertyUpkeepService {
	@Autowired
	FileService fileService;
	@Autowired
	PropertyUpkeepMapper propertyUpkeepMapper;
	@Autowired
	ISysUserService sysUserService;
	@Autowired
	PropertyService propertyService;

   /**
	 * 查询所有
	 */
	@Override
	public List<PropertyUpkeepVO> getAll(){
		List<PropertyUpkeep> list = this.list();
		List<PropertyUpkeepVO> result = BeanUtil.copyToList(list, PropertyUpkeepVO.class);
		return result;
	}

	/**
     * 分页查询
     */
	@Override
    public Page<PropertyUpkeep> getPage(PropertyUpkeepDTO dto){
		//创建分页对象
		Page<PropertyUpkeep> page = new Page(dto.getPageNum(), dto.getPageSize());
		//dto查询条件转分页条件
		LambdaQueryWrapper<PropertyUpkeep> lqw = new LambdaQueryWrapper<>();
		Page<PropertyUpkeep> result = propertyUpkeepMapper.selectPage(page, lqw);
		return result;
	}

	/**
	 * 添加
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Long add(PropertyUpkeepAddOrUpdDTO dto){
		Property property = propertyService.getById(dto.getPropertyId());
		if(Constants.YES.equals(property.getIsIdle())){
			throw new ServiceException("该资产已闲置，无法添加维修记录");
		}

		//添加维修记录
		PropertyUpkeep propertyUpkeep = BeanUtil.copyProperties(dto, PropertyUpkeep.class);
		propertyUpkeep.setUserId(UserContext.getUser().getUserId());
		this.save(propertyUpkeep);

		if(!CollectionUtils.isEmpty(dto.getFileIds())){
			fileService.addSave(propertyUpkeep.getId(),dto.getFileIds());
		}

		//修改最近维修记录时间
		Property propertyUpd = new Property();
		propertyUpd.setId(dto.getPropertyId());
		propertyUpd.setLatelyUpkeep(dto.getUpkeepTime());
		propertyService.updateById(propertyUpd);

		return propertyUpkeep.getId();
	}

	@Override
	public List<PropertyUpkeepVO> getByPropertyId(Integer propertyId){
		LambdaQueryWrapper<PropertyUpkeep> lqw = new LambdaQueryWrapper<>();
		lqw.eq(PropertyUpkeep::getPropertyId,propertyId);
		lqw.eq(PropertyUpkeep::getDeleteFlag, Constants.NO);
		List<PropertyUpkeep> list = this.list(lqw);
		if(CollUtil.isEmpty(list)){
			return new ArrayList<>();
		}
		List<PropertyUpkeepVO> result = BeanUtil.copyToList(list, PropertyUpkeepVO.class);

		List<Long> ids = result.stream()
				.map(i -> i.getId())
				.collect(Collectors.toList());
		Map<Long, List<File2VO>> fileMap = fileService.getMap(FileType.TypeEnum.FILE_TYPE_1002.getCode(), ids);

		List<Long> userIds = result.stream()
				.filter(i -> !NumberUtils.longEmpty(i.getUserId()))
				.map(i -> i.getUserId())
				.collect(Collectors.toList());
		Map<Long, SysUser> userMap = sysUserService.getMapByIds(userIds);

		for(PropertyUpkeepVO i:result){
			if(fileMap.containsKey(i.getId())){
				i.setFiles(fileMap.get(i.getId()));
			}
			i.setUserName(sysUserService.getNameByMap(userMap,i.getUserId()));
		}
		return result;
	}

	/**
	 * 通过主键修改
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void upd(PropertyUpkeepAddOrUpdDTO dto){
		PropertyUpkeep propertyUpkeep = BeanUtil.copyProperties(dto, PropertyUpkeep.class);
		propertyUpkeep.setUserId(UserContext.getUser().getUserId());
		this.updateById(propertyUpkeep);

		fileService.updSave(FileType.TypeEnum.FILE_TYPE_1002.getCode(),propertyUpkeep.getId(),dto.getFileIds());

		//修改最近维修记录时间
		Property propertyUpd = new Property();
		propertyUpd.setId(dto.getPropertyId());
		propertyUpd.setLatelyUpkeep(dto.getUpkeepTime());
		propertyService.updateById(propertyUpd);
	}

	@Override
	public void del(Long id){
		if(NumberUtils.longEmpty(id)){
			throw new ServiceException("参数id不能为空");
		}

		PropertyUpkeep propertyUpkeep = new PropertyUpkeep();
		propertyUpkeep.setId(id);
		propertyUpkeep.setDeleteFlag(Constants.YES);
		this.updateById(propertyUpkeep);
	}

}



